package kernel;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * System core
 *
 */
public class AsuvizCore {

	private Map<String, FiberSetLite> fiberSetIndex;
	private Map<String, Volume> volumeIndex;
	private static AsuvizCore instance;
	private String statsRelativePath;

	/**
	 * Gets an instance for the system
	 * @return
	 */
	public static AsuvizCore getInstance()
	{
		if(instance == null)
		{
			instance = new AsuvizCore();
		}
		return instance;
	}

	/**
	 * Constructor
	 */
	private AsuvizCore()
	{
		fiberSetIndex = new HashMap<String, FiberSetLite>();
		volumeIndex = new HashMap<String, Volume>();
	}

	public void setStatRelativePath(String nStatsRelativePath)
	{
		statsRelativePath = nStatsRelativePath;
	}

	public void createVolumeFromFile(File file, Color color) throws Exception
	{
		Volume volume = new Volume();
		volume.readFile(file, color);
		volumeIndex.put(file.getAbsolutePath(), volume);
		
		double volumeInMm = readStats(file);
		if(volumeInMm == -1)
		{
//			throw new Exception("No structure found in file");
			volume.setVolume(0);
		}
		else
		{
			volume.setVolume(volumeInMm);
		}
		
	}

	private double readStats(File file) throws Exception
	{
		double volumeInMm = -1;
		String absolutePath = file.getAbsolutePath();
		String structureName = absolutePath.substring(absolutePath.lastIndexOf(File.separatorChar) + 1);
		structureName = structureName.substring(0, structureName.lastIndexOf("."));
		absolutePath = absolutePath.substring(0,absolutePath.lastIndexOf(File.separatorChar));
		absolutePath = absolutePath + statsRelativePath;
		File statsFile = new File(absolutePath);
		BufferedReader reader = new BufferedReader(new FileReader(statsFile));
		String line = "";
		while((line = reader.readLine())!= null)
		{
			if(line.startsWith("#"))
			{
				continue;
			}
			else
			{
				line = line.trim();
				String[] lineContent = line.split("\\s+");
				String structureNameInFile = lineContent[4];
				structureNameInFile = structureNameInFile.replace("-", "");
				if(structureName.equalsIgnoreCase(structureNameInFile))
				{
					volumeInMm = Double.parseDouble(lineContent[3]);
					break;
				}
			}
		}
		reader.close();
		return volumeInMm;
	}

	public ArrayList<Volume> getVolumes()
	{
		ArrayList<Volume> volumes = new ArrayList<Volume>();
		for(Map.Entry<String, Volume> entry : volumeIndex.entrySet())
		{
			volumes.add(entry.getValue());
		}
		return volumes;
	}

	public void createFiberFromFile(File file, Color color) throws Exception
	{
		FiberSetLite fiberSet = new FiberSetLite();
		fiberSet.readFile(file, color);
		fiberSetIndex.put(file.getAbsolutePath(), fiberSet);
	}

	/**
	 * Returns the map with the information of all the fiber sets that are in the system
	 * @return fiber set index
	 */
	public Map<String, FiberSetLite> getFiberSetIndex() {
		return fiberSetIndex;
	}

	public FiberSetLite getFiberByName(String name)
	{
		return fiberSetIndex.get(name);
	}

	public Volume getVolumeByName(String name)
	{
		return volumeIndex.get(name);
	}

	public double getStructureVolumeInMm(String volumeName) {
		return volumeIndex.get(volumeName).getVolume();
	}
}
